/*
 * Created on Jun 28, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package filesystem.local;

import filesystem.FSException;
import filesystem.MyFile;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;

import javax.swing.filechooser.FileSystemView;


public class MyLocalFileSystem {
	private FileSystemView view=FileSystemView.getFileSystemView();
		
	/**
	 *
	 */
	public MyLocalFileSystem() {
		super();
	}

    public char getPathSeparator() {
		return File.pathSeparatorChar;
	}

	public char getSeparator() {
		return File.separatorChar;
	}

	private MyFile[] getJFMArrayFromFiles(File[] files){
		if(files==null) return null;		
		MyFile[] myFiles =new MyFile[files.length];
		for(int i=0;i<files.length;i++){
			try{
				myFiles[i]=new MyLocalFile(files[i].getAbsolutePath());
			}catch(Exception ignored){
				ignored.printStackTrace();
			}
		}
		Arrays.sort(myFiles);
		return myFiles;
	}


	public MyFile getDefaultRootDirectory(){
		//return as the users default root dir the root dir of the users home dir				
		return getRootDriveFile(new MyLocalFile(view.getHomeDirectory()));
	}

	public MyFile[] listRoots() {
		File[] roots=File.listRoots();
		return getJFMArrayFromFiles(roots);
	}

	public MyFile[] listFiles(MyFile root) {
		File f=new File(root.getAbsolutePath());
		File[] localFiles=view.getFiles(f,true);
		return getJFMArrayFromFiles(localFiles);
	}


	public MyFile[] listFiles(MyFile rootFile,FilenameFilter filter) {
		File f=new File(rootFile.getAbsolutePath());
		
		File[] localFiles=f.listFiles(filter);
		return getJFMArrayFromFiles(localFiles);
	}

	public boolean mkdir(MyFile file) {
		File f=new File(file.getAbsolutePath());
		return f.mkdir();
	}

	public boolean mkdirs(MyFile file) {
		File f=new File(file.getAbsolutePath());
		return f.mkdirs();
	}

	public boolean delete(MyFile file) {
		File f=new File(file.getAbsolutePath());
		return f.delete();
	}

	public boolean createNewFile(MyFile file) throws FSException {
		File f=new File(file.getAbsolutePath());
		try{
			return f.createNewFile();
		}catch(IOException ex){
			throw new FSException(ex.getMessage());
		}
	}

	public boolean setLastModified(MyFile file, long time) {
		File f=new File(file.getAbsolutePath());
		return f.setLastModified(time);
	}

	public boolean setReadOnly(MyFile file) {
		File f=new File(file.getAbsolutePath());
		return f.setReadOnly();
	}

	public boolean canRead(MyFile file) {
		File f=new File(file.getAbsolutePath());
		return f.canRead();
	}

	public boolean canWrite(MyFile file) {
		File f=new File(file.getAbsolutePath());
		return f.canWrite();
	}

	public boolean exists(MyFile file) {
		File f=new File(file.getAbsolutePath());
		return f.exists();
	}

        public MyFile getStartDirectory(){
          return getFile(System.getProperty("user.dir"));
        }

        public MyFile getFile(String pathName){
          return new MyLocalFile(pathName);
        }

		public boolean mkdir(MyFile parent, String name) {
			File parentFile=new File(parent.getAbsolutePath(),name);			
			return parentFile.mkdir();
		}

		public boolean createNewFile(MyFile parent, String name) throws FSException {
			File f=new File(parent.getAbsolutePath(),name);
			try{
				return f.createNewFile();
			}catch(IOException ex){
				throw new FSException(ex.getMessage());
			}
		}
		
		public boolean isLocal()
		{
			return true;		
		}

    public MyFile getRootDriveFile(MyFile file){
      MyFile parent=file.getParentFile();
      if(parent==null) return file;

      return getRootDriveFile(parent);
    }

    public String getPathSeparatorString(){
    	return ""+getPathSeparator();
    }
}
